//
//  TopicCommentTableViewCell.swift
//  HoneyBee
//
//  Created by Smith_Yang on 2018/5/30.
//  Copyright © 2018 point. All rights reserved.
//

import UIKit

class TopicCommentTableViewCell: UITableViewCell {

    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var commentButton: UIButton!
    @IBOutlet weak var upIconImageView: UIImageView!
    @IBOutlet weak var upCount: UILabel!
    @IBOutlet weak var replayCount: UIButton!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var usernameLabel: UILabel!
    @IBOutlet weak var avatarImageView: UIImageView!
    
    var block : ((_ btn:UIButton) -> ())?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.selectionStyle = .none
        replayCount.layer.cornerRadius = replayCount.frame.size.height/2.0
       
        upIconImageView.image = UIImage.icon(from: kIcon_up, color: kColorTextPlacehold, imageSize: CGSize(width: 15, height: 14)).withRenderingMode(.alwaysOriginal)
        
        let replayImg = UIImage.icon(from: kIcon_replay, color: kColorTextPlacehold, imageSize: CGSize(width: 15, height: 14)).withRenderingMode(.alwaysOriginal)
        commentButton.setImage(replayImg, for: .normal)
        
        avatarImageView.layer.cornerRadius = 10
        avatarImageView.layer.masksToBounds = true
        avatarImageView.contentMode = .scaleAspectFit
        commentButton.addTarget(self, action : #selector(didClick(btn:)), for: .touchUpInside)
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    public func configModel(model:TopicCommentModel, didClickComment:@escaping((_ btn:UIButton) -> ())) {
        
        contentLabel.text = model.reply
        
//        avatarImageView.setImageWith(URL(fileURLWithPath: model.headImg))
        
        block = didClickComment
        
        avatarImageView.kf.setImage(with: URL(string: model.headImg), placeholder: UIImage.init(named: "defalut_avatar"), options: [.cacheMemoryOnly], progressBlock: nil, completionHandler: nil);
        
        usernameLabel.text = model.userName
        
        dateLabel.text = String.timeStringToShow(str: model.createTime)
        
        replayCount.setTitle(String(format: "%d回复", model.replyNum), for: .normal)
        
        
    }
   @objc private func didClick(btn:UIButton){
    weak var weakBtn = btn
        if block != nil {
            block!(weakBtn!)
        }
    }
    
}
